<?php
//session_start(); 
//error_reporting(E_ALL & ~E_NOTICE);
function GetTumblrMedia($GET)
{
	
if ($GET['Reset']=='true')
	$offset=0;
//Name of TUmble Blog
if ($GET['uname'] == ''){
           	$uname = 'beautifulweddings' ;
} else {
	    	$uname = $GET['uname'];
			}
    
//Limit of number of blog entries
if ($GET['limit'] == ''){
           	$limit = 20 ;
} else {
	    	$limit = $GET['limit'];
			}
			


	
if(isset($_SESSION['offset']))
	$_SESSION['offset']=$_SESSION['offset']+$limit;
else
	$_SESSION['offset']=0;


if((isset($_SESSION['uname']))&&($_SESSION['uname'] == $uname)){
	$offset=$_SESSION['offset'];
} else {
	$_SESSION['uname'] = $uname;
	$_SESSION['offset']=0;
}


	/*		
			
    if ($_GET['offset'] == ''){
            $offset = '0' ;
    }  
     
            else {
                    $offset = $_GET['offset'];};
     */
     
    $key = '2cEGYXKZpBh6tH7ria3NsJu95AXhqvGDLeWMRfTXCC2rpAlVq5' ;
     
    #Get offset from URL
      $postID = '' ;
    
     
    #Generate Tumblr URL
    $request = 'http://api.tumblr.com/v2/blog/'.$uname.'.tumblr.com/posts/?api_key='.$key.$postID.'&offset='.$offset.'&limit='.$limit ;
     
            #Get Tumblr JSON
            $ci = curl_init($request);
            curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
            $input = curl_exec($ci);
     
            #Puts JSON into Array
            $value = json_decode($input,true);
     
            #Check if Tumblr JSON is correct
            if ($value['meta']['msg'] == 'OK'){
     
                    #Process JSON Array
                    $posts = $value['response']['posts'];
                    $pCount = $value['response']['blog']['posts'];
     
                    #echo($pCount);
                    #var_dump($value);
     				if ($pCount < ($limit+$offset)){
						$limit = $pCount - $offset;	
					}
					$MediaList = array();
                    #Create Post Dump
     				//echo "\n\n<MediaLists>\n";
                    for($i='0';$i<=($limit - 1);$i++){
                            $inf = $posts[$i];
     						array_push ($MediaList, MakeMediaArray_Tumblr($uname,$inf));
							/**echo "<Media ID=\"tb_".$uname."_".$inf['id']."\" MediaType=\"".$inf['type']."\" CommentCount=\"\" WebLink=\"".$inf['post_url']."\">\n";
								if (array_key_exists('body', $inf)) {
									echo "<Text ID=\"\" Title=\"".XMLCharEncode($inf['title'])."\" Description=\"".XMLCharEncode($inf['body'])."\"/>\n";
								}
								if ($inf['type'] == 'quote') {
									echo "<Text ID=\"\" Title=\"".XMLCharEncode($inf['text'])."\" Description\"".XMLCharEncode($inf['source'])."\"/>\n";
								}
								if ($inf['type'] == 'link') {
									echo "<Text ID=\"\" Title=\"".XMLCharEncode($inf['title'])."\" Description\"".XMLCharEncode($inf['url'])."\"/>\n";
								}
								if (array_key_exists('photos', $inf)) { 
									$ftype = substr($inf['photos'][0]['alt_sizes'][0][url],-3);
									 echo "<Image ID=\"\" Type=\"$ftype\" FileName=\"".$inf['photos'][0]['alt_sizes'][0][url]."\" Description=\"".XMLCharEncode($inf['caption'])."\"/>\n";
								}
     						echo "</Media>\n\n";**/
                    };
					return $MediaList;
     				//echo "</MediaLists>";
                   
                   
            } ;

}     
	function MakeMediaArray_Tumblr($uname,$inf)
	{
		$Media = array();
		
		$MediaDetial = array( "Head"=>"Media", "ID"=>"", "BeMediaID"=>"Tumblr_".$uname."_".$inf['id'], "MediaType"=>$inf['type'], "CommentCount"=>"", "WebLink"=>$inf['post_url'], "Source"=>"Tumblr", "Author"=>"","lastUpdate"=>$inf['timestamp']);
		array_push( $Media, $MediaDetial );
		
		//------------------------Media-text Info------------------------//
		if (array_key_exists('body', $inf)) {
			$MediaDetial = array( "Head"=>"Text", "ID"=>"tb_".$uname."_".$inf['id'], "Title"=>strip_tags($inf['title']), "Description"=>strip_tags($inf['body']));
		array_push( $Media, $MediaDetial );
		}
		else if ($inf['type'] == 'quote') {
			$MediaDetial = array( "Head"=>"Text", "ID"=>"tb_".$uname."_".$inf['id'], "Title"=>strip_tags($inf['text']), "Description"=>strip_tags($inf['source']));
			array_push( $Media, $MediaDetial );
		}
		else if ($inf['type'] == 'link') {
			$MediaDetial = array( "Head"=>"Text", "ID"=>"tb_".$uname."_".$inf['id'], "Title"=>strip_tags($inf['title']), "Description"=>strip_tags($inf['url']));
			array_push( $Media, $MediaDetial );
		}
		else if ($inf['type'] == 'photo') {
			$MediaDetial = array( "Head"=>"Text", "ID"=>"tb_".$uname."_".$inf['id'], "Title"=>strip_tags($inf['caption']), "Description"=>"");
			array_push( $Media, $MediaDetial );
		}
		else if ($inf['type'] == 'video') {
			$MediaDetial = array( "Head"=>"Text", "ID"=>"tb_".$uname."_".$inf['id'], "Title"=>strip_tags($inf['caption']), "Description"=>"");
			array_push( $Media, $MediaDetial );
			$MediaDetial = array( "Head"=>"Video", "ID"=>"", "FileName"=>strip_tags($inf['permalink_url']), "Description"=>"");
			array_push( $Media, $MediaDetial );
		}
		
		//------------------------Media-image Info------------------------//
		if (array_key_exists('photos', $inf)) {
			//$ftype = substr($inf['photos'][0]['alt_sizes'][0]['url'],-3);
			$MediaDetial = array( "Head"=>"Image_W_H", "ID"=>"", "Type"=>'URL', "Width"=>$inf['photos'][0]['alt_sizes'][0]['width'], "Height"=>$inf['photos'][0]['alt_sizes'][0]['height'], "FileName"=>$inf['photos'][0]['alt_sizes'][0]['url'], "Description"=>$inf['caption']);
			array_push( $Media, $MediaDetial );
		}
		return $Media;
	}
  /**function XMLCharEncode( $Subject )
	{
		$Serach  = array( "<", ">", "&", "'","\"" );
		$Replace = array( "&lt;","&gt;","&amp;","&apos;","&quot;");
		return str_replace($Serach, $Replace, $Subject);
	} **/ 

?>